import {observable, extendObservable, action, toJS} from 'mobx';
import {
  getRecognitionRecordInfo,
  deleteUserInfo,
  getDataListInfo,
} from 'util/api';
import _ from 'lodash';
import {validateFields} from '../Common.js';
import {message} from 'antd';
import store from 'store';

//导出
export default class HouseholdStore {
  constructor() {
    this.reset(true);
  }

  @action
  reset = init => {
    const state = {
      tableData: [],
      loading: false,
      // apartmentList: [],
      tableParams: {},
      userType: '',
      name: '',
      startTime: '',
      endTime: '',
      recordLocation: '',
    };

    if (init) {
      extendObservable(this, state);
    } else {
      Object.keys(state).forEach(key => (this[key] = state[key]));
    }
  };

  @action
  onWillMount = async (location, match, history) => {
    this.reset();

    this.loading = true;
    await this.getRecognitionRecordInfo();
    this.loading = false;
  };
  @action
  getRecognitionRecordInfo = async (
    params = {
      pageNo: 1,
      pageSize: 10,
      userType: '',
      recordLocation: '',
      name: '',
      startTime: '',
      endTime: '',
    },
  ) => {
    this.loading = true;
    const res = await getRecognitionRecordInfo(params);
    this.loading = false;
    if (res.result === 'success') {
      this.tableParams = {...this.tableParams, ...params};
      this.tableData = res;
    } else {
      message.error(res.msg);
    }
  };
  @action
  changeRecordLocation = async pos => {
    this.my_recordLocation = pos;
  };
  @action
  changeName = async name => {
    this.my_name = name;
  };
  @action
  changeUserType = async userType => {
    this.my_userType = userType;
  };
  @action
  changeRangePicker = async date => {
    this.my_date = date;
  };
  @action
  search = async () => {
    let userType = this.my_userType || '';
    let name = this.my_name || '';
    let startTime = this.my_date ? this.my_date[0] : '';
    let endTime = this.my_date ? this.my_date[1] : '';
    let recordLocation = this.my_recordLocation || '';
    this.userType = userType;
    this.recordLocation = recordLocation;
    this.name = name;
    this.startTime = startTime;
    this.endTime = endTime;
    let params = {
      pageNo: 1,
      pageSize: 10,
      ...this.tableParams,
      userType,
      name,
      startTime,
      endTime,
      recordLocation,
    };
    this.getRecognitionRecordInfo(params);
  };

  @action
  onDelete = async record => {
    let res = await deleteUserInfo({
      userId: record.userId,
      name: record.name,
      userType: record.userType,
    });
    if (res.result === 'success') {
      this.getUserInfo();
    } else {
      message.error(res.msg);
    }
  };
}
